package com.example.xd.mvp_project.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.xd.mvp_project.R;

/**
 * @auther XuDan on 2018/10/17.
 */
public class Title extends RelativeLayout {

    private RelativeLayout mTitleLeftRl;
    private RelativeLayout mTitleRightRl;

    public Title(Context context) {
        this(context, null);
    }

    public Title(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public Title(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.title_layout, this, true);
        RelativeLayout titleBg = findViewById(R.id.title_bg);
        mTitleLeftRl = findViewById(R.id.title_left_rl);
        ImageView titleLeftImg = findViewById(R.id.title_left_img);
        TextView titleContentTv = findViewById(R.id.title_content_tv);
        mTitleRightRl = findViewById(R.id.title_right_rl);
        ImageView titleRightImg = findViewById(R.id.title_right_img);
        TextView titleRightTV = findViewById(R.id.title_right_tv);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Title);
        if (typedArray != null) {
            int title_bg_id = typedArray.getResourceId(R.styleable.Title_title_bg, R.layout.title_layout);
            titleBg.setBackgroundResource(title_bg_id);
            boolean is_show_left_rl = typedArray.getBoolean(R.styleable.Title_is_show_left_rl, false);
            if (is_show_left_rl) {
                mTitleLeftRl.setVisibility(VISIBLE);
            } else {
                mTitleLeftRl.setVisibility(GONE);
            }
            int left_img_id = typedArray.getResourceId(R.styleable.Title_left_img_id, -1);
            if (left_img_id != -1) {
                titleLeftImg.setImageResource(left_img_id);
            }

            String title_content = typedArray.getString(R.styleable.Title_title_content);
            if (!TextUtils.isEmpty(title_content)) {
                titleContentTv.setText(title_content);
            }

            boolean is_show_right_rl = typedArray.getBoolean(R.styleable.Title_is_show_right_rl, false);
            if (is_show_right_rl) {
                mTitleRightRl.setVisibility(VISIBLE);
            } else {
                mTitleRightRl.setVisibility(GONE);
            }

            boolean is_show_right_img = typedArray.getBoolean(R.styleable.Title_is_show_right_img, false);
            if (is_show_right_img) {
                titleRightImg.setVisibility(VISIBLE);
            } else {
                titleRightImg.setVisibility(GONE);
            }

            int right_img_id = typedArray.getResourceId(R.styleable.Title_right_img_id, -1);
            if (right_img_id != -1) {
                titleRightImg.setImageResource(right_img_id);
            }

            boolean is_show_right_tv = typedArray.getBoolean(R.styleable.Title_is_show_right_tv, false);
            if (is_show_right_tv) {
                titleRightTV.setVisibility(VISIBLE);
            } else {
                titleRightTV.setVisibility(GONE);
            }

        }
        typedArray.recycle();
    }

    public void setTitleClickListener(OnClickListener listener) {
        if (listener != null) {
            mTitleLeftRl.setOnClickListener(listener);
            mTitleRightRl.setOnClickListener(listener);
        }
    }
}
